package com.zzvcom.webapp.action.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.admin.Meta;
import com.zzvcom.persist.model.admin.Product;
import com.zzvcom.persist.model.admin.ProductDes;
import com.zzvcom.service.admin.MetaManager;
import com.zzvcom.service.admin.ProductDesManager;
import com.zzvcom.service.admin.ProductManager;
import com.zzvcom.template.ResultPage;
import com.zzvcom.utils.DateUtil;
import com.zzvcom.utils.ExtMessage;

@Controller
@Scope("prototype")
public class ProductAction
{

    private Product product;

    private ProductManager manager;

    private ExtMessage msg = new ExtMessage();// 返回信息

    private Map rs = new HashMap();// 结果集合

    private ResultPage resultPage;

    private Integer start;

    private Integer limit;

    public String execute()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        resultPage.startLimitSetting(start, limit);
        DetachedCriteria dc = DetachedCriteria.forClass(Product.class);
        if(product !=null && StringUtils.isNotBlank(product.getNo()))
        {
            dc.add(Restrictions.like("no", product.getNo(), MatchMode.START));
        }        
        if(product !=null && StringUtils.isNotBlank(product.getCategoryid()))
        {
            dc.add(Restrictions.eq("categoryid", product.getCategoryid()));
        }        
        dc.addOrder(Order.desc("addtime"));
        resultPage.setDetachedCriteria(dc);
        manager.getResultPageByDetachedCriteria(resultPage);
        rs.put("root", resultPage.getResult());
        rs.put("records", resultPage.getTotalRecord());
        return "rs";
    }

    public String save()
    {
        if (StringUtils.isBlank(product.getId()))
        {
            product.setId(null);            
            product.setAddtime(DateUtil.dateToNum14(new Date()));
        }        
        manager.saveProduct(product);
        msg.setInfo("保存成功");
        return "msg";
    }
    
    String pid;
    
    ProductDes des;
    
    ProductDesManager pdm;
    
    MetaManager mm;
    
    List<Meta> metas=new ArrayList<Meta>();
    
    
    public String fetchDes()
    {
        des=(ProductDes)pdm.queryUniqueByHQL("from ProductDes where productid=?", pid);        
        return "des";
    }
    
    public String fetchMetas()
    {
        metas=mm.queryByHQL("from Meta where productid=?", pid);
        return "metas";
    }
    

    public String delete()
    {        
        if(product.getId().indexOf('/')<0)
        {
            manager.deleteProduct(product.getId());
        }
        else
        {
            for(String id:product.getId().split("/"))
            {                
                manager.deleteProduct( id );
            }
            
        }
        
        msg.setInfo("删除成功");
        return "msg";
    }
    
    public String clearIsolate()
    {        
        manager.clearIsolate();
        msg.setInfo("清除成功！");
        return "msg";
    }

    public ResultPage getResultPage()
    {
        return resultPage;
    }

    public void setResultPage(ResultPage resultPage)
    {
        this.resultPage = resultPage;
    }

    public Product getProduct()
    {
        return product;
    }

    public void setProduct(Product product)
    {
        this.product = product;
    }

    public ProductManager getManager()
    {
        return manager;
    }

    @Autowired
    public void setManager(ProductManager manager)
    {
        this.manager = manager;
    }

    public ExtMessage getMsg()
    {
        return msg;
    }

    public void setMsg(ExtMessage msg)
    {
        this.msg = msg;
    }

    public Map getRs()
    {
        return rs;
    }

    public void setRs(Map rs)
    {
        this.rs = rs;
    }

    public Integer getStart()
    {
        return start;
    }

    public void setStart(Integer start)
    {
        this.start = start;
    }

    public Integer getLimit()
    {
        return limit;
    }

    public void setLimit(Integer limit)
    {
        this.limit = limit;
    }

    public String getPid()
    {
        return pid;
    }

    public void setPid(String pid)
    {
        this.pid = pid;
    }

    public ProductDes getDes()
    {
        return des;
    }

    public void setDes(ProductDes des)
    {
        this.des = des;
    }

    public ProductDesManager getPdm()
    {
        return pdm;
    }
    @Autowired
    public void setPdm(ProductDesManager pdm)
    {
        this.pdm = pdm;
    }

    public MetaManager getMm()
    {
        return mm;
    }
    @Autowired
    public void setMm(MetaManager mm)
    {
        this.mm = mm;
    }

    public List<Meta> getMetas()
    {
        return metas;
    }

    public void setMetas(List<Meta> metas)
    {
        this.metas = metas;
    }
    
    

}
